<?php

namespace app\modules\v2\models;

use Yii;
use yii\db\ActiveRecord;

/**
 * This is the model class for table "sys_auth".
 *
 * @property int $authid
 * @property string|null $app_code
 * @property string|null $app_secret
 * @property string|null $auth_key
 * @property string|null $access_token
 * @property int $allowance
 * @property int $allowance_updated_at
 * @property int $deleteflag
 */
class SysAuth extends ActiveRecord implements \yii\filters\RateLimitInterface
{

    /**
     * @var int
     */
    private $rateLimit = 3;

    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return 'sys_auth';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['allowance', 'allowance_updated_at', 'deleteflag'], 'integer'],
            [['app_code', 'app_secret', 'auth_key', 'access_token'], 'string', 'max' => 50],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'authid' => 'Authid',
            'app_code' => 'App Code',
            'app_secret' => 'App Secret',
            'auth_key' => 'Auth Key',
            'access_token' => 'Access Token',
            'allowance' => 'Allowance',
            'allowance_updated_at' => 'Allowance Updated At',
            'deleteflag' => 'Deleteflag',
        ];
    }
    /**
     * Returns the maximum number of allowed requests and the window size.
     * @param \yii\web\Request $request the current request
     * @param \yii\base\Action $action the action to be executed
     * @return array an array of two elements. The first element is the maximum number of allowed requests,
     * and the second element is the size of the window in seconds.
     */
    public function getRateLimit($request, $action)
    {
        // 限速每秒3次
        return [$this->rateLimit, 1];
    }

    /**
     * Loads the number of allowed requests and the corresponding timestamp from a persistent storage.
     * @param \yii\web\Request $request the current request
     * @param \yii\base\Action $action the action to be executed
     * @return array an array of two elements. The first element is the number of allowed requests,
     * and the second element is the corresponding UNIX timestamp.
     */
    public function loadAllowance($request, $action)
    {
        return [$this->allowance, $this->allowance_updated_at];
    }

    /**
     * Saves the number of allowed requests and the corresponding timestamp to a persistent storage.
     * @param \yii\web\Request $request the current request
     * @param \yii\base\Action $action the action to be executed
     * @param int $allowance the number of allowed requests remaining.
     * @param int $timestamp the current timestamp.
     */
    public function saveAllowance($request, $action, $allowance, $timestamp)
    {
        $this->allowance = $allowance;
        $this->allowance_updated_at = $timestamp;
        $this->save();
    }
}
